/**
 * 鉴别列。在继承关系体系中，配置在父类上，用于区分子类
 */
import tnx from '@/tnx.js';
import DiscriminatorType from '../enums/DiscriminatorType.js';

export default class DiscriminatorColumn {

    name = '';
    type = DiscriminatorType.CHAR;
    length = 1;

    constructor(name, type) {
        this.name = name;
        this.type = DiscriminatorType.validate(type);
    }

    static of(object) {
        if (typeof object !== 'object') {
            return undefined;
        }
        let discriminator = new DiscriminatorColumn(object.name, object.type);
        if (object.length) {
            discriminator.length = parseInt(object.length) || 1;
        }
        return discriminator;
    }

    update(object) {
        return tnx.util.object.rewriteSimpleDefined(this, object);
    }

    clone() {
        return DiscriminatorColumn.of(this);
    }

}
